﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Navigation;
using GranitePackageSystem.GranitePackagingService;


namespace GranitePackageSystem.Views.Request
{
    public partial class TestingDetails : Page
    {
        GranitePackagingService.Request currentRequest;
        private string exePath = string.Empty;
        private string VMName;
        private string[] PreBuildStatusArray = new string[] { "New", "Found Virtual Machine", "Taking VM Snapshot", "Adding User to Local Groups", "Entitling User to View Pool", "Powering on Virtual Machine" };   
        public TestingDetails()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            checkRoles();
            if(App.Current.Resources.Contains("Current Request")) 
            {
                currentRequest = (GranitePackagingService.Request) App.Current.Resources["Current Request"];
                updateRequestNavTable();

                if (currentRequest.Stage.Equals("Staging"))
                {
                    SetStatus();
                }
                else
                {
                    this.NavigationService.Navigate(new Uri("/Home", System.UriKind.Relative)); 
                }
            }
            else 
            { 
                this.NavigationService.Navigate(new Uri("/Home", System.UriKind.Relative)); 
            }
        }

        private void SetStatus()
        {
            String currentStatus = currentRequest.Status.ToUpper();
            if (currentStatus.Equals("VM DEPLOYED"))
            {
                busyIndicatorTesting.IsBusy = true;
                busyIndicatorTesting.BusyContent = "Getting Virtual Machine Details";

                btnDeployFiles.Visibility = System.Windows.Visibility.Collapsed;
                btnDeployFiles.IsEnabled = false;

                fileUploader.Visibility = System.Windows.Visibility.Visible;
                fileUploader.IsEnabled = true;

                image.Source = new BitmapImage(new Uri("/GranitePackageSystem;component/Assets/Resources/UploadSourceFiles.png", UriKind.RelativeOrAbsolute));

                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.getRequestVirtualMachineCompleted += new EventHandler<getRequestVirtualMachineCompletedEventArgs>(proxy_getRequestVirtualMachineCompleted);
                proxy.getRequestVirtualMachineAsync(currentRequest);
            }
            else if (currentStatus.Equals("FILES UPLOADED"))
            {
                busyIndicatorTesting.IsBusy = true;
                busyIndicatorTesting.BusyContent = "Loading Request Data";

                panelUploadedFiles.Visibility = System.Windows.Visibility.Visible;
                panelExeBrowser.Visibility = System.Windows.Visibility.Visible;
                fileUploader.Visibility = System.Windows.Visibility.Collapsed;

                btnDeployFiles.Visibility = System.Windows.Visibility.Collapsed;
                btnDeployFiles.IsEnabled = false;

                image.Source = new BitmapImage(new Uri("/GranitePackageSystem;component/Assets/Resources/SelectInstallExe.png", UriKind.RelativeOrAbsolute));

                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.getSourceFileObjectCompleted += new EventHandler<getSourceFileObjectCompletedEventArgs>(proxy_getSourceFileObjectCompleted);
                proxy.getSourceFileObjectAsync(currentRequest.requestID);
            }
            else if (currentStatus.Equals("RUN FILE SELECTED") || currentStatus.Equals("INSTALLING SOFTWARE ON VM"))
            {
                busyIndicatorTesting.IsBusy = true;
                busyIndicatorTesting.BusyContent = "Loading Request Data";

                panelExeSelected.Visibility = System.Windows.Visibility.Visible;
                panelUploadedFiles.Visibility = System.Windows.Visibility.Visible;
                fileUploader.Visibility = System.Windows.Visibility.Collapsed;
                panelExeBrowser.Visibility = System.Windows.Visibility.Collapsed;

                btnDeployFiles.Visibility = System.Windows.Visibility.Visible;
                btnDeployFiles.IsEnabled = true;

                image.Source = new BitmapImage(new Uri("/GranitePackageSystem;component/Assets/Resources/VDIActions.png", UriKind.RelativeOrAbsolute));

                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.getPackageListCompleted += new EventHandler<getPackageListCompletedEventArgs>(proxy_getPackageListCompleted_Setup);
                proxy.getPackageListAsync(currentRequest);
            }
            // What to do in an intermitent state -- Pool until it gets to a 'final' state
            else
            {
                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.getRequestCompleted += new EventHandler<getRequestCompletedEventArgs>(proxy_getRequestCompleted_init);
                busyIndicatorTesting.BusyContent = currentRequest.Status;
                busyIndicatorTesting.IsBusy = true;
                proxy.getRequestAsync(currentRequest.requestID, 0);
            }
        }
        private void checkRoles()
        {
            Web.User user = WebContext.Current.User;
            bool userInARole = false;

            if (user.IsInRole("GENMILLS\\GPS_Requesters")) { userInARole = true; }
            if (user.IsInRole("GENMILLS\\GPS_Administrators")) { userInARole = true; }
            if (user.IsInRole("GENMILLS\\GPS_LicensingApprovers")) { userInARole = true; }
            if (user.IsInRole("GENMILLS\\GPS_DeploymentApprovers")) { userInARole = true; }

            if (userInARole) { panelContent.Visibility = System.Windows.Visibility.Visible; }
            else { this.NavigationService.Navigate(new System.Uri("/Unauthorized", System.UriKind.Relative)); }
        }

        void proxy_getRequestCompleted_init(object sender, getRequestCompletedEventArgs e)
        {
            if (e.Result.Stage.Equals("Staging"))
            {
                App.Current.Resources.Remove("Current Request");
                App.Current.Resources.Add("Current Request", e.Result);
                currentRequest = e.Result;

                SetStatus();
            }
            else
            {
                this.NavigationService.Navigate(new System.Uri("/Home", System.UriKind.Relative));
            }
        }

        private void updateRequestNavTable()
        {
            DateTime MIN_SQL_DATE = (DateTime)App.Current.Resources["Min SQL Date"];
            expanderRequest.Visibility = System.Windows.Visibility.Visible;
            if (currentRequest.Stage.Equals("Staging"))
            {
                linkTestingDetails.Visibility = System.Windows.Visibility.Visible;
                if (currentRequest.VirtStepComplete.CompareTo(MIN_SQL_DATE) > 0)
                {
                    linkPackageDetails.Visibility = System.Windows.Visibility.Visible;
                    if (currentRequest.PackageStepComplete.CompareTo(MIN_SQL_DATE) > 0)
                    {
                        linkLicensingDetails.Visibility = System.Windows.Visibility.Visible;
                        if (currentRequest.LicensingStepComplete.CompareTo(MIN_SQL_DATE) > 0)
                        {
                            linkSummaryDetails.Visibility = System.Windows.Visibility.Visible;
                        }
                        else
                        {
                            linkSummaryDetails.Visibility = System.Windows.Visibility.Collapsed;
                        }
                    }
                    else
                    {
                        linkLicensingDetails.Visibility = System.Windows.Visibility.Collapsed;
                        linkSummaryDetails.Visibility = System.Windows.Visibility.Collapsed;
                    }
                }
                else
                {
                    linkLicensingDetails.Visibility = System.Windows.Visibility.Collapsed;
                    linkSummaryDetails.Visibility = System.Windows.Visibility.Collapsed;
                    linkPackageDetails.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
            else
            {
                linkLicensingDetails.Visibility = System.Windows.Visibility.Collapsed;
                linkPackageDetails.Visibility = System.Windows.Visibility.Collapsed;
                linkTestingDetails.Visibility = System.Windows.Visibility.Collapsed;
                linkSummaryDetails.Visibility = System.Windows.Visibility.Visible;
            }
        }
        
        void proxy_getRequestVirtualMachineCompleted_LoadDefault(object sender, getRequestVirtualMachineCompletedEventArgs e)
        {
            image.Source = new BitmapImage(new Uri("/GranitePackageSystem;component/Assets/Resources/UploadSourceFiles.png", UriKind.RelativeOrAbsolute));

            busyIndicatorTesting.IsBusy = false;
            blockVMName.Text = e.Result.Name;
        }
        void proxy_getPackageListCompleted_Setup(object sender, getPackageListCompletedEventArgs e)
        {
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();

            if (e.Result.Count > 0)
            {
                proxy.getProgramListCompleted += new EventHandler<getProgramListCompletedEventArgs>(proxy_getProgramListCompleted_Setup);
                proxy.getProgramListAsync(e.Result[0]);
            }
            else
            {
                throw new Exception();
            }
        }
        void proxy_getProgramListCompleted_Setup(object sender, getProgramListCompletedEventArgs e)
        {
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();

            if (e.Result.Count > 0)
            {
                proxy.getRequestVirtualMachineCompleted += new EventHandler<getRequestVirtualMachineCompletedEventArgs>(proxy_getRequestVirtualMachineCompleted);
                proxy.getRequestVirtualMachineAsync(currentRequest);
                txtExePath.Text = e.Result[0].CommandLine.ToString();
            }
            else
            {
                throw new Exception();
            }
        }
        private void requestVM()
        {
            busyIndicatorTesting.IsBusy = true;
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.requestVMCompleted += new EventHandler<requestVMCompletedEventArgs>(proxy_requestVMCompleted);
            proxy.requestVMAsync(currentRequest);
        }
        void proxy_requestVMCompleted(object sender, requestVMCompletedEventArgs e)
        {
            if (e.Result.Status.Equals("Rejected"))
            {
                ChildWindows.RejectionWindow rejectWindow = new ChildWindows.RejectionWindow();
                rejectWindow.Closed += new EventHandler(rejectWindow_Closed);
                rejectWindow.Show();
            }
            else
            {
                GranitePackagingService.Request req = currentRequest;

                req.Status = "VM Deployed";
                req = setRequestTimeToUTC(req);

                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();   
                proxy.saveRequestCompleted += new EventHandler<saveRequestCompletedEventArgs>(proxy_saveRequestCompleted_VMDeployed);
                proxy.saveRequestAsync(req);
            }
        }
        void rejectWindow_Closed(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new System.Uri("/Home", System.UriKind.Relative));
        }
        void proxy_saveRequestCompleted_VMDeployed(object sender, saveRequestCompletedEventArgs e)
        {
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.getRequestVirtualMachineCompleted += new EventHandler<getRequestVirtualMachineCompletedEventArgs>(proxy_getRequestVirtualMachineCompleted);
            proxy.getRequestVirtualMachineAsync(currentRequest);
        }
        

        void childWindow_Closed_NoVMS_INIT(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Home", UriKind.Relative));
        }

        private void fileUploader_Loaded(object sender, RoutedEventArgs e)
        {
            this.fileUploader.Files.SingleFileUploadFinished += new EventHandler<FileUploader.FileUploadedEventArgs>(Files_SingleFileUploadFinished);
            //this.fileUploader.Files.AllFilesFinished += new EventHandler(Files_AllFilesFinished);
        }

        void Files_SingleFileUploadFinished(object sender, FileUploader.FileUploadedEventArgs e)
        {
            currentRequest.Status = "Files Uploaded";
            App.Current.Resources.Remove("Current Request");
            App.Current.Resources.Add("Current Request",currentRequest);

            busyIndicatorTesting.IsBusy = true;
            busyIndicatorTesting.BusyContent = "Unzipping uploaded content";

            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.InnerChannel.OperationTimeout = TimeSpan.FromMinutes(60);
            proxy.processFileCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_processFileCompleted);
            proxy.processFileAsync(e.FileGuid, e.FileName, currentRequest.requestID);
            
            proxy.saveRequestAsync(currentRequest);
        }

        void proxy_processFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            panelUploadedFiles.Visibility = System.Windows.Visibility.Visible;
            fileUploader.Visibility = System.Windows.Visibility.Collapsed;
            panelExeBrowser.Visibility = System.Windows.Visibility.Visible;

            GranitePackagingService.Request req = (GranitePackagingService.Request)App.Current.Resources["Current Request"];
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.InnerChannel.OperationTimeout = TimeSpan.FromMinutes(60);
            proxy.getSourceFileObjectCompleted += new EventHandler<getSourceFileObjectCompletedEventArgs>(proxy_getSourceFileObjectCompleted);
            proxy.getSourceFileObjectAsync(req.requestID);
        }

        void proxy_getSourceFileObjectCompleted(object sender, getSourceFileObjectCompletedEventArgs e)
        {
            image.Source = new BitmapImage(new Uri("/GranitePackageSystem;component/Assets/Resources/SelectInstallExe.png", UriKind.RelativeOrAbsolute));
            busyIndicatorExeBrowser.IsBusy = false;
            treeViewFolerList.ItemsSource = e.Result;

            busyIndicatorTesting.IsBusy = false;

            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.getRequestVirtualMachineCompleted += new EventHandler<getRequestVirtualMachineCompletedEventArgs>(proxy_getRequestVirtualMachineCompleted);
            proxy.getRequestVirtualMachineAsync(currentRequest);
        }
        void proxy_getRequestVirtualMachineCompleted(object sender, getRequestVirtualMachineCompletedEventArgs e)
        {
            blockVMName.Text = e.Result.Name;
            busyIndicatorTesting.IsBusy = false;
        }

        private GranitePackagingService.Request setRequestTimeToUTC(GranitePackagingService.Request req)
        {
            req.AdvertStepComplete = req.AdvertStepComplete.ToUniversalTime();
            req.DeploymentStepComplete = req.DeploymentStepComplete.ToUniversalTime();
            req.EnteredTime = req.EnteredTime.ToUniversalTime();
            req.FinishedTime = req.FinishedTime.ToUniversalTime();
            req.LastUpdateTime = req.LastUpdateTime.ToUniversalTime();
            req.LicensingStepComplete = req.LicensingStepComplete.ToUniversalTime();
            req.PackageStepComplete = req.PackageStepComplete.ToUniversalTime();
            req.ProgramStepComplete = req.ProgramStepComplete.ToUniversalTime();
            req.SourceStepComplete = req.SourceStepComplete.ToUniversalTime();
            req.VirtStepComplete = req.VirtStepComplete.ToUniversalTime();

            return req;
        }

        private void fileUploader_UploadStarted(object sender, EventArgs e)
        {
            
        }

        private void btnChangeSourceFiles_Click(object sender, RoutedEventArgs e)
        {
            busyIndicatorTesting.IsBusy = true;
            busyIndicatorTesting.BusyContent = "Removing old Source Files";

            btnDeployFiles.Visibility = System.Windows.Visibility.Collapsed;
            btnDeployFiles.IsEnabled = false;

            panelDeployedFiles.Visibility = System.Windows.Visibility.Collapsed;

            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.teardownRequestCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_teardownRequestCompleted);
            proxy.teardownRequestAsync(currentRequest.requestID);
        }

        void proxy_teardownRequestCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            currentRequest.Status = "VM Deployed";
            App.Current.Resources.Remove("Current Request");
            App.Current.Resources.Add("Current Request", currentRequest);
            
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.getPackageListCompleted += new EventHandler<getPackageListCompletedEventArgs>(proxy_getPackageListCompleted_Teardown);
            proxy.getPackageListAsync(currentRequest);
            
        }

        void proxy_getPackageListCompleted_Teardown(object sender, getPackageListCompletedEventArgs e)
        {
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            foreach (Package p in e.Result)
            {
                proxy.deleteProgramAsync(p);
            }
            proxy.deletePackageCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_deletePackageCompleted_Teardown);
            proxy.deletePackageAsync(currentRequest);
            proxy.saveRequestCompleted += new EventHandler<saveRequestCompletedEventArgs>(proxy_saveRequestCompleted_Teardown);
            proxy.saveRequestAsync(currentRequest);
        }

        void proxy_deletePackageCompleted_Teardown(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.saveRequestCompleted += new EventHandler<saveRequestCompletedEventArgs>(proxy_saveRequestCompleted_Teardown);
            proxy.saveRequestAsync(currentRequest);
        }

        void proxy_saveRequestCompleted_Teardown(object sender, saveRequestCompletedEventArgs e)
        {
            panelUploadedFiles.Visibility = System.Windows.Visibility.Collapsed;
            panelExeBrowser.Visibility = System.Windows.Visibility.Collapsed;
            panelExeSelected.Visibility = System.Windows.Visibility.Collapsed;
            fileUploader.ClearFileList();
            
            fileUploader.IsEnabled = true;
            fileUploader.Visibility = System.Windows.Visibility.Visible;

            image.Source = new BitmapImage(new Uri("/GranitePackageSystem;component/Assets/Resources/UploadSourceFiles.png", UriKind.RelativeOrAbsolute));
            busyIndicatorTesting.IsBusy = false;
        }

        private void btnSetExecutable_Click(object sender, RoutedEventArgs e)
        {
            if (FileList.SelectedItem != null)
            {
                busyIndicatorTesting.IsBusy = true;
                busyIndicatorTesting.BusyContent = "Setting Request Executable";

                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.getPackageListCompleted += new EventHandler<getPackageListCompletedEventArgs>(proxy_getPackageListCompleted);
                proxy.getPackageListAsync(currentRequest);
            }
            else
            {
                MessageBox.Show("You must select an installation file");
            }
        }

        void proxy_getPackageListCompleted(object sender, getPackageListCompletedEventArgs e)
        {
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            if (e.Result.Count > 0)
            {
                Package p = e.Result[0];
                string cmdLine = string.Empty;

                SourceFolder current = treeViewFolerList.SelectedItem as SourceFolder;

                cmdLine += ((SourceFile)FileList.SelectedItem).Path + "\\" + ((SourceFile)FileList.SelectedItem).DisplayName;

                proxy.createProgramCompleted += new EventHandler<createProgramCompletedEventArgs>(proxy_createProgramCompleted);
                proxy.createProgramAsync(cmdLine, "", p.PackageID, currentRequest);
            }
            else
            {
                proxy.createPackageCompleted +=new EventHandler<createPackageCompletedEventArgs>(proxy_createPackageCompleted);
                proxy.createPackageAsync(currentRequest);
            }
        }

        void proxy_createPackageCompleted(object sender, createPackageCompletedEventArgs e)
        {
            string cmdLine = string.Empty;

            SourceFolder current = treeViewFolerList.SelectedItem as SourceFolder;

            cmdLine += ((SourceFile)FileList.SelectedItem).Path + "\\" + ((SourceFile)FileList.SelectedItem).DisplayName;

            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.createProgramCompleted += new EventHandler<createProgramCompletedEventArgs>(proxy_createProgramCompleted);
            proxy.createProgramAsync(cmdLine, "", e.Result.PackageID, currentRequest);
        }

        void proxy_createProgramCompleted(object sender, createProgramCompletedEventArgs e)
        {
            txtExePath.Text = e.Result.CommandLine;         

            currentRequest.Status = "Run File Selected";
            App.Current.Resources.Remove("Current Request");
            App.Current.Resources.Add("Current Request",currentRequest);

            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.saveRequestCompleted += new EventHandler<saveRequestCompletedEventArgs>(proxy_saveRequestCompleted_CreateProgram);
            proxy.saveRequestAsync(currentRequest);
        }

        void proxy_saveRequestCompleted_CreateProgram(object sender, saveRequestCompletedEventArgs e)
        {
            image.Source = new BitmapImage(new Uri("/GranitePackageSystem;component/Assets/Resources/VDIActions.png", UriKind.RelativeOrAbsolute));
            busyIndicatorTesting.IsBusy = false;

            panelExeBrowser.Visibility = System.Windows.Visibility.Collapsed;
            panelExeSelected.Visibility = System.Windows.Visibility.Visible;

            btnDeployFiles.Visibility = System.Windows.Visibility.Visible;
            btnDeployFiles.IsEnabled = true;
        }

        private void btnChangeExecutable_Click(object sender, RoutedEventArgs e)
        {
            busyIndicatorTesting.IsBusy = true;
            busyIndicatorTesting.BusyContent = "Removing old Executable";

            btnDeployFiles.Visibility = System.Windows.Visibility.Collapsed;
            btnDeployFiles.IsEnabled = false;

            panelDeployedFiles.Visibility = System.Windows.Visibility.Collapsed;

            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.getPackageListCompleted += new EventHandler<getPackageListCompletedEventArgs>(proxy_getPackageListCompleted_ChangeExe);
            proxy.getPackageListAsync(currentRequest);
        }

        void proxy_getPackageListCompleted_ChangeExe(object sender, getPackageListCompletedEventArgs e)
        {
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();

            if (e.Result.Count > 0)
            {
                proxy.deleteProgramCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_deleteProgramCompleted_ChangeExe);
                proxy.deleteProgramAsync(e.Result[0]);
            }
            else
            {
                throw new Exception();
            }
        }

        void proxy_deleteProgramCompleted_ChangeExe(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            currentRequest.Status = "Files Uploaded";
            App.Current.Resources.Remove("Current Request");
            App.Current.Resources.Add("Current Request", currentRequest);

            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.saveRequestCompleted += new EventHandler<saveRequestCompletedEventArgs>(proxy_saveRequestCompleted_ChangeExe);
            proxy.saveRequestAsync(currentRequest);
        }

        void proxy_saveRequestCompleted_ChangeExe(object sender, saveRequestCompletedEventArgs e)
        {
            panelExeBrowser.Visibility = System.Windows.Visibility.Visible;
            panelExeSelected.Visibility = System.Windows.Visibility.Collapsed;
            image.Source = new BitmapImage(new Uri("/GranitePackageSystem;component/Assets/Resources/SelectInstallExe.png", UriKind.RelativeOrAbsolute));
            busyIndicatorTesting.IsBusy = false;

            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();

            proxy.getSourceFileObjectCompleted += new EventHandler<getSourceFileObjectCompletedEventArgs>(proxy_getSourceFileObjectCompleted);
            proxy.getSourceFileObjectAsync(currentRequest.requestID);
        }

        private void btnDeployFiles_Click(object sender, RoutedEventArgs e)
        {            
            busyIndicatorTesting.IsBusy = true;
            busyIndicatorTesting.BusyContent = "Deploying Selected Items to Virtual Machine";

            image.Source = new BitmapImage(new Uri("/GranitePackageSystem;component/Assets/Resources/VDIActions.png", UriKind.RelativeOrAbsolute));

            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.getRequestVirtualMachineCompleted += new EventHandler<getRequestVirtualMachineCompletedEventArgs>(proxy_getRequestVirtualMachineCompleted_AccessVDI);
            proxy.getRequestVirtualMachineAsync(currentRequest);
        }

        void proxy_getRequestVirtualMachineCompleted_AccessVDI(object sender, getRequestVirtualMachineCompletedEventArgs e)
        {
            try
            {
                VMName = e.Result.Name;
                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 60, 0);
                proxy.get32BitRegistryARPEntryNamesCompleted += new EventHandler<get32BitRegistryARPEntryNamesCompletedEventArgs>(proxy_get32BitRegistryARPEntryNamesCompleted_AccessVDI);
                proxy.get32BitRegistryARPEntryNamesAsync(e.Result.Name);
            }
            catch { MessageBox.Show("Error Accessing VDI Please try again later"); this.NavigationService.Navigate(new Uri("/Home", UriKind.Relative)); }
        }

        void proxy_get32BitRegistryARPEntryNamesCompleted_AccessVDI(object sender, get32BitRegistryARPEntryNamesCompletedEventArgs e)
        {
            try
            {
                currentRequest.ARPList_32 = e.Result;
                App.Current.Resources.Remove("Current Request");
                App.Current.Resources.Add("Current Request", currentRequest);

                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 60, 0);
                proxy.get64BitRegistryARPEntryNamesCompleted += new EventHandler<get64BitRegistryARPEntryNamesCompletedEventArgs>(proxy_get64BitRegistryARPEntryNamesCompleted_AccessVDI);
                proxy.get64BitRegistryARPEntryNamesAsync(VMName);
            }
            catch { MessageBox.Show("Error Accessing VDI Please try again later"); this.NavigationService.Navigate(new Uri("/Home", UriKind.Relative)); }
        }

        void proxy_get64BitRegistryARPEntryNamesCompleted_AccessVDI(object sender, get64BitRegistryARPEntryNamesCompletedEventArgs e)
        {
            try
            {
                currentRequest.Status = "Deploy to VM";
                currentRequest.ARPList_64 = e.Result;
                currentRequest.VirtStepComplete = DateTime.UtcNow;
                App.Current.Resources.Remove("Current Request");
                App.Current.Resources.Add("Current Request", currentRequest);

                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.saveRequestCompleted += new EventHandler<saveRequestCompletedEventArgs>(proxy_saveRequestCompleted_AccessVDI);
                proxy.saveRequestAsync(currentRequest);
            }
            catch { MessageBox.Show("Error Accessing VDI Please try again later"); this.NavigationService.Navigate(new Uri("/Home", UriKind.Relative)); }
        }

        void proxy_saveRequestCompleted_AccessVDI(object sender, saveRequestCompletedEventArgs e)
        {
            updateRequestNavTable();

            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();

            proxy.getRequestCompleted += new EventHandler<getRequestCompletedEventArgs>(proxy_getRequestCompleted_AccessVDI);
            proxy.getRequestAsync(currentRequest.requestID, 5);
        }
        
        void proxy_getRequestCompleted_AccessVDI(object sender, getRequestCompletedEventArgs e)
        {
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();

            if (e.Result.Status.Equals("Installing Software on VM"))
            {
                System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("https://vdi.genmills.com"), "_newWindow");
                this.NavigationService.Navigate(new Uri("/Request/PackageDetails", System.UriKind.Relative));
            }
            else
            {
                busyIndicatorTesting.BusyContent = e.Result.Status.ToString();
                proxy.getRequestCompleted += new EventHandler<getRequestCompletedEventArgs>(proxy_getRequestCompleted_AccessVDI);
                proxy.getRequestAsync(currentRequest.requestID, 5);
            }
        }
    }
}
